$(document).ready(function(){
	var type = $("#admin_ourwork_item").attr('type');
	var title = $(".title").text();
	$(".save_txt").click(function(){
		var ID=$(this).attr('id');
		var title = $("#title_input_"+ID).val();
		var description = $("#des_input_"+ID).val();
		if (type == 'client' || type == 'portfolio' ) {
		  var url_ytb = $("#ytb_input_"+ID).val();
		  var youtube_id = "";
		  if (!youtube_parser(url_ytb)) {
				youtube_id = url_ytb;			  
		  } else {
			  youtube_id = youtube_parser(url_ytb);
		  }
			  if (description != "" || title != "" || url_ytb != "") {
				  $.ajax({
				  url: baseurl + "admin_ourworks/editItem/"+type,
				  type: "post",
				  data:{'youtube_id':youtube_id,'description':description,'ID':ID,'title':title},
				  success: function(str){
					  var string = $.parseJSON(str);
						  if (string.success == false) {
							  alert(string.reason);
						  }else{
								  $("#title_"+ID).text(title);		
								  $("#des_"+ID).text(description);
								  $("#"+ID+" .value > a").attr("href", "http://www.youtube.com/watch?v="+string.id);
								  $("#"+ID+" .value > a > img").attr("src", "http://i1.ytimg.com/vi/"+string.id+"/maxresdefault.jpg");
								  $("#"+ID+" .value > a > p").text(string.id);
								  $("#"+ID+" .edit_txt").toggle();
								  $("#"+ID+" .save_txt").toggle();
								  $("#"+ID+" .cancel_txt").toggle();
								  $("#"+ID+" .value").toggle();
								  $("#"+ID+" .input").toggle();
						  }
				  },
					  error: function(str){alert('Error!! Please contact the administrator.');}
				  });
				  }else{
						  alert('Please enter both description and youtube_id');
				  }
		} else {
			var newImg = $("#newImg_"+ID).val();
			var img = $("#curImg_"+ID).attr('src').substring(baseurl.length)
			if (newImg) {
				img = newImg;
			}
			if (description != "" || title != "") {
					$.ajax({
					url: baseurl + "admin_ourworks/editItem/"+type,
					type: "post",
					data:{'img':img,'description':description,'ID':ID,'title':title},
					success: function(str){
						var string = $.parseJSON(str);
							if (string.success == false) {
								alert(string.reason);
							}else{
								$("#title_"+ID).text(title);	
								$("#des_"+ID).text(description);
								$("#curImg_"+ID).attr("src", baseurl + img);
								$("#PhotoPrevs_"+ID).empty();
								$("#"+ID+" .edit_txt").toggle();
								$("#"+ID+" .save_txt").toggle();
								$("#"+ID+" .cancel_txt").toggle();
								$("#"+ID+" .value").toggle();
								$("#"+ID+" .input").toggle();
							}
					},
						error: function(str){alert('Error!! Please contact the administrator.');}
					});
					}else{
						alert('Please enter description');
					}
				}
	});
	
	$("#submit_new").click(function(){
	  var title =  $("#title").val();
	  var description =  $("#description").val();
	  if (description != "" && title !="") {
		  if (type == 'client' || type == 'portfolio' ) {
			var url_ytb = $("#ytb_id").val();
			var youtube_id = "";
			if (!youtube_parser(url_ytb)) {
				  youtube_id = url_ytb;			  
			} else {
				youtube_id = youtube_parser(url_ytb);
			}
			  $.ajax({
				url: baseurl + "admin_ourworks/addItem/"+type,
				type: "post",
				data:{'title':title,'description':description,'youtube_id':youtube_id,},
				success: function(str){
					var string = $.parseJSON(str);
						if (string.success == false) {
							alert(string.reason);
						}else{
							location.reload();
						}
				},
					error: function(str){alert('Error!! Please contact the administrator.');}
			   });
		  } else {
			  var newImg = $("#newImg").val();
			  $.ajax({
				url: baseurl + "admin_ourworks/addItem/"+type,
				type: "post",
				data:{'title':title,'description':description,'newImg':newImg,},
				success: function(str){
					var string = $.parseJSON(str);
						if (string.success == false) {
							alert(string.reason);
						}else{
							location.reload();
						}
				},
					error: function(str){alert('Error!! Please contact the administrator.');}
			   });
		  }  
	  }else{
		  alert('Please enter both title and description');
	  }
  });
  
  $(".delete_all_rows").click(function(){
	  var r=confirm("Are you to delete the records?");
	  if (r==true) {
		var ids = new Array();
		$("input[@name=row_sel]:checked" ).each(function() {
		  var id=$(this).attr('id');
		  ids.push(id);
		});
		$.ajax({
			url: baseurl + "admin_ourworks/deleteItem",
			type: "post",
			data:{'ids':ids},
			//dataType: 'json',
			success: function(str){
				var string = $.parseJSON(str);
					if (string.success == false) {
						alert(string.reason);
					}else{
						//$("#des_"+ID).text(description);
						ids.forEach(function(entry) {
							$("tr#"+entry).empty();
						});
						alert('Sucessfully delete selected rows');
					}
			},
				error: function(str){alert('Error!! Please contact the administrator.');}
			});
	  } else {
	  
	  }
  });
	
  $('.add_new').magnificPopup({
  type: 'inline',
  midClick: true,
  closeBtnInside: true,
  closeOnBgClick: false,
  });
  
});


 
